package com.mc.fastkit.span

import android.graphics.LinearGradient
import android.graphics.Shader
import android.text.TextPaint
import android.text.style.CharacterStyle
import android.text.style.UpdateAppearance

/**
 * 文字渐变
 * @author: MasterChan
 * @date: 2025-08-13 13:25
 */
class TextGradientSpan(
    private val text: CharSequence,
    private val colors: IntArray,
    private val position: FloatArray? = null,
    private val isVertical: Boolean = false
) : CharacterStyle(), UpdateAppearance {

    override fun updateDrawState(tp: TextPaint) {
        val shader = if (isVertical) {
            LinearGradient(
                0f, 0f, 0f, tp.descent() - tp.ascent(),
                colors,
                position,
                Shader.TileMode.REPEAT
            )
        } else {
            LinearGradient(
                0f, 0f, tp.measureText(text.toString()), 0f,
                colors,
                position,
                Shader.TileMode.REPEAT
            )
        }
        tp.shader = shader
    }
}